package com.f5.management.service.impl;


import com.f5.management.dao.CommunityDAO;
import com.f5.management.entity.Community;
import com.f5.management.service.CommunityService;
import com.f5.management.utils.PageResult;
import com.f5.management.utils.SerialUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CommunityServiceImpl implements CommunityService {
    @Autowired
    private CommunityDAO communityDAO;

    //添加
    @Override
    public boolean add(Community community) {
        //自动生成编号    C1001
        int num = communityDAO.count();
        //生成的 n + 1
        community.setSerialNumber(SerialUtil.CommunitySerial(num + 1));

        return communityDAO.add(community) > 0 ? true : false;
    }

    //更改状态
    @Override
    @Transactional
    public boolean updateStatus(int id, boolean status) {

        return communityDAO.updateStatus(id, status) > 0 ? true : false;
    }

    //分页查询 首页展示
    @Override
    public PageResult<List<Community>> findByPage(int page, int size) {
        PageResult<List<Community>> result = new PageResult<List<Community>>();
        Page<Community> startPage = PageHelper.startPage(page, size);
        List<Community> communities = communityDAO.findAll();
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(startPage.getTotal());
        result.setTotalPage(startPage.getPages());
        result.setData(communities);
        return result;
    }

    //更改信息
    @Override
    public boolean updateInfo(Community community) {
        return communityDAO.updateInfo(community) > 0 ? true : false;
    }

    //查询编号是否存在
    @Override
    public boolean selectNumber(String serialNumber) {
        String oldNum = serialNumber;
        String newNum = communityDAO.selectNumber(serialNumber);
        //查询编号是否存在
        if (!oldNum.equals(newNum)) {
            return true;
        } else {
            return false;
        }
    }

    //删除信息
    @Override
    public boolean delInfo(int id) {
        return communityDAO.delInfo(id) > 0 ? true : false;
    }

    //通过名字查询数据
    @Override
    public PageResult<List<Community>> findByName(String serialNumber,String name, int page, int size) {
        PageResult<List<Community>> result = new PageResult<List<Community>>();
        Page<Community> pageHelper = PageHelper.startPage(page, size);
        List<Community> communities = communityDAO.findByName(serialNumber,name);
        result.setCurrentPage(page);
        result.setSize(size);
        result.setTotal(pageHelper.getTotal());
        result.setTotalPage(pageHelper.getPages());
        result.setData(communities);
        return result;
    }

    //通过小区名字查询cid
    @Override
    public int findCidByName(String name) {
        return communityDAO.findCidByName(name);
    }

    //id查询信息进行更改
    @Override
    public Community findById(int id) {

        return communityDAO.findById(id);
    }
}

















